const {
	Controller
} = require('uni-cloud-router')
const db = uniCloud.database()

module.exports = class EnterApplyController extends Controller {
	// 查询商家入驻信息
	async querySettledInfo() {
		let data = this.ctx.data
		try {
			let result = await this.service['enter-apply'].querySettledInfo({
				uid: this.ctx.auth.uid
			})
			let res = result.data[0]
			// console.log(res);
			if (res) {
				res.joinRegion = res.region.province.label + res.region.city.label + res.region.area.label
				delete res.region
				delete res._id
				delete res.createTime
				return {
					code: 200,
					result: res
				}
			} else {
				return {
					code: 200,
					result: {
						name: '',
						mobile: '',
						storeName: '',
						joinRegion: '',
						location: {
							longitude: '',
							latitude: ''
						},
						address: '',
					}
				}
			}
		} catch (e) {
			return {
				code: 500,
				result: e
			}
		}
	}

	// 商家入驻
	async enterApply() {
		let data = this.ctx.data
		// console.log(this.ctx.data);
		if (!data.name || !data.mobile || !data.storeName || !data.region || !data.location || !data.location.longitude || !parseFloat(data.location.longitude) || !data.location.latitude || !parseFloat(data.location.latitude)) {
			return {
				code: 500,
				msg: '参数不正确'
			}
		}
		data.location = new db.Geo.Point(data.location.longitude, data.location.latitude)
		data.createTime = new Date().getTime()
		try {
			await this.service['enter-apply'].enterApply(data)
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
		return
	}
}
